﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using MvcJqGrid;
using PagedList;
using Microsoft.Office.Interop.Excel;
using WebApp.Commons;
using WebApp.Data;
using WebApp.Services;
using System.Globalization;

namespace WebApp.Controllers
{
    public class ProjectAreaTypeController : Controller
    {
        private ProjectAreaTypeService _service;
        //
        // GET: /ProjectAreaType/

        public ActionResult Index(string keyword)
        {
            _service = new ProjectAreaTypeService();

            keyword = _service.GetProjAreaName();

            return View();
        }

        public JsonResult List(GridSettings gridSettings, string keyword)
        {
            _service = new ProjectAreaTypeService();

            var realCount = 0;
            
            var datas = _service.GetAreaType(gridSettings.PageIndex,
                                          gridSettings.PageSize,
                                          gridSettings.SortColumn,
                                          gridSettings.SortOrder,
                                          keyword,
                                          ref realCount);

            var totalBUs = realCount;
            var totalPage = totalBUs / gridSettings.PageSize;

            if (totalPage > Convert.ToInt32(totalPage))
            {
                totalPage = totalPage + 1;
            }

            var jsonData = new
            {
                total = Convert.ToInt32(totalPage),
                page = gridSettings.PageIndex,
                records = totalBUs,
                rows = (
                            from c in datas
                            select new
                            {
                                id = c.area_type_id,
                                cell = new[] {
                                            c.area_type_id.ToString(CultureInfo.InvariantCulture),
                                            c.area_type_name,
                                            c.area_type_create_date.HasValue ? c.area_type_create_date.Value.ToLongDateString() : ""
                                            }
                            }
                        ).ToPagedList(1, gridSettings.PageSize).ToArray()
            };

            return Json(jsonData, JsonRequestBehavior.AllowGet);
        }

        #region | For SubGrid |

        public JsonResult SubList(int typeId, GridSettings gridSettings, string keyword)
        {
            _service = new ProjectAreaTypeService();

            var realCount = 0;

            var datas = _service.GeSteps(typeId,
                                          gridSettings.PageIndex,
                                          gridSettings.PageSize,
                                          gridSettings.SortColumn,
                                          gridSettings.SortOrder,
                                          keyword,
                                          ref realCount);

            var totalBUs = realCount;
            var totalPage = totalBUs / gridSettings.PageSize;

            if (totalPage > Convert.ToInt32(totalPage))
            {
                totalPage = totalPage + 1;
            }

            var jsonData = new
            {
                total = Convert.ToInt32(totalPage),
                page = gridSettings.PageIndex,
                records = totalBUs,
                rows = (
                            from c in datas
                            let firstOrDefault = DataProvider.GetStep().FirstOrDefault(s => s.step_id.Equals(c.step_id))
                            where firstOrDefault != null
                            select new
                            {
                                id = c.area_type_step_id,
                                cell = new[] {
                                            c.area_type_step_id.ToString(CultureInfo.InvariantCulture),
                                            c.step_id.ToString(CultureInfo.InvariantCulture),
                                            firstOrDefault != null ? firstOrDefault.step_name : string.Empty,
                                            "10",
                                            c.order_no.ToString(CultureInfo.InvariantCulture)
                                            }
                            }
                        ).ToPagedList(1, gridSettings.PageSize).ToArray()
            };

            return Json(jsonData, JsonRequestBehavior.AllowGet);
        }

        public String GetStep()
        {
            var sTag = DataProvider.GetStep().Aggregate("<select>", (current, type) => current + "<option value='" + type.step_id + "'>" + type.step_name + "</option>");

            return sTag + "</select>";
        }

        public JsonResult SubUpdate(int typeId, tbl_step oModel, int order, FormCollection formCollection)
        {
            _service = new ProjectAreaTypeService();

            var isPass = false;
            var iMessage = string.Empty;

            var operation = formCollection["oper"];

            if (operation.Equals("add") || operation.Equals("edit"))
            {
                isPass = _service.SubUpdate(typeId, oModel, order);
            }

            return Json(new { isPass, iMessage });
        }

        public JsonResult SubDelete(int[] id, FormCollection formCollection)
        {
            _service = new ProjectAreaTypeService();

            var isPass = false;
            var iMessage = string.Empty;

            var operation = formCollection["oper"];

            if (operation.Equals("del"))
            {
                isPass = _service.SubDelete(id);
            }

            return Json(new { isPass, iMessage });
        }

        #endregion

        #region | Inner Sub |

        public JsonResult InnerSubList(int typeStepId, GridSettings gridSettings, string keyword)
        {
            _service = new ProjectAreaTypeService();

            var realCount = 0;

            var datas = _service.GeStepsMatType(typeStepId,
                                          gridSettings.PageIndex,
                                          gridSettings.PageSize,
                                          gridSettings.SortColumn,
                                          gridSettings.SortOrder,
                                          keyword,
                                          ref realCount);

            var totalBUs = realCount;
            var totalPage = totalBUs / gridSettings.PageSize;

            if (totalPage > Convert.ToInt32(totalPage))
            {
                totalPage = totalPage + 1;
            }

            var jsonData = new
            {
                total = Convert.ToInt32(totalPage),
                page = gridSettings.PageIndex,
                records = totalBUs,
                rows = (
                            from c in datas
                            let firstOrDefault = DataProvider.GetMaterialType().FirstOrDefault(s => s.material_type_id.Equals(c.material_type_id))
                            where firstOrDefault != null
                            select new
                            {
                                id = c.area_type_step_material_id,
                                cell = new[] {
                                            c.area_type_step_material_id.ToString(CultureInfo.InvariantCulture),
                                            c.material_type_id.ToString(),
                                            firstOrDefault != null ? firstOrDefault.material_type_name : string.Empty,
                                            c.used_material_quatity.ToString(CultureInfo.InvariantCulture)
                                            }
                            }
                        ).ToPagedList(1, gridSettings.PageSize).ToArray()
            };

            return Json(jsonData, JsonRequestBehavior.AllowGet);
        }

        public String GetMatType()
        {
            var sTag = DataProvider.GetMaterialType().Aggregate("<select>", (current, type) => current + "<option value='" + type.material_type_id + "'>" + type.material_type_name + "</option>");

            return sTag + "</select>";
        }

        public JsonResult InnerSubUpdate(int typeStepId, tbl_material_type oModel, int qty, FormCollection formCollection)
        {
            _service = new ProjectAreaTypeService();

            var isPass = false;
            var iMessage = string.Empty;

            var operation = formCollection["oper"];

            if (operation.Equals("add") || operation.Equals("edit"))
            {
                isPass = _service.InnerSubUpdate(typeStepId, oModel, qty);
            }

            return Json(new { isPass, iMessage });
        }

        public JsonResult InnerSubDelete(int[] id, FormCollection formCollection)
        {
            _service = new ProjectAreaTypeService();

            var isPass = false;
            var iMessage = string.Empty;

            var operation = formCollection["oper"];

            if (operation.Equals("del"))
            {
                isPass = _service.InnerSubDelete(id);
            }

            return Json(new { isPass, iMessage });
        }

        #endregion

    }
}
